from owlready2 import *

print('1. adding an annotation')
onto = get_ontology("http://test.org/onto.owl")
with onto:
    class Drug(Thing):
        pass
    Drug.comment = ["A first comment on the Drug class", "A second comment"]
    Drug.comment.append("A third comment")
    class HealthProblem(Thing):
        pass
    class is_prescribed_for(Drug >> HealthProblem):
        pass
    acetaminophen = Drug("acetaminophen")
    pain = HealthProblem("pain")
    acetaminophen.is_prescribed_for.append(pain)
    comment[acetaminophen, is_prescribed_for, pain] = "A comment on the acetaminophen-pain relation"
    comment[Drug, rdfs_subclassof, Thing] = "A comment on an is-a relation"
    acetaminophen.comment = "This comment replaces all existing comments on acetaminophen"



print('2. querying annotations')
with onto:
    print('\t', Drug.comment)
    print('\t', comment[acetaminophen, is_prescribed_for, pain])
    print('\t', comment[Drug, rdfs_subclassof, Thing])
    print('\t', acetaminophen.comment.first())


print('3. Deleting annotations')
with onto:
    Drug.comment.remove("A second comment")
    Drug.comment = []


print('4. Custom rendering of entities')

def render_using_label(entity):
    return entity.label.first() or entity.name

def render_using_iri(entity):
    return entity.iri

with onto:
    set_render_func(render_using_label)
    print('\t', Drug)
    Drug.label = "The drug class"
    print(Drug)
    set_render_func(render_using_iri)
    print('\t', Drug)



print('5. Language-specific annotations')
with onto:
    Drug.comment = [ locstr("Un commentaire en Français", lang = "fr"),
                    locstr("A comment in English", lang = "en") ]
    print('\t', Drug.comment[0])
    print('\t', Drug.comment[0].lang)
    print('\t', Drug.comment.fr)
    print('\t', Drug.comment.en)
    print('\t', Drug.comment.en.first())
    Drug.comment.en.append("A second English comment")
    lang = "fr"
    print('\t', Drug.comment.get_lang(lang))
    lang = "fr"
    print('\t', Drug.comment.get_lang_first(lang))



print('6. Creating new classes of annotation')

with onto:
    class my_annotation(AnnotationProperty):
        pass
    class pharmaceutical_comment(comment):
        pass
    acetaminophen.pharmaceutical_comment = "A comment related to pharmacology of acetaminophen"



print('7. Full-text search (FTS)')
default_world.full_text_search_properties.append(label)
print('\t', default_world.search(label = FTS("drug")))
print('\t', default_world.search(label = FTS("drug"), _bm25 = True))